const viewManager = require('../../../utils/view-manager')
const cache = require('../../../utils/cache')
const { format } = require('util')

function send({ node, helps, template, isCommand }) {
    const options = cache.connections.get(node.host)
    const temp = isCommand ? template + ' \r' : template
    viewManager.openWithFunction('shell', node.host, options, 'onCommand', temp, helps, isCommand)
}

module.exports = {
    id: 'docker-compose',
    menus: [
        {
            label: '上线',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'scale',
                    label: '实例数量',
                    type: 'text',
                    placeholder: 'ServiceName=3',
                    defaultValue: '',
                    required: false
                },
                {
                    name: 'detach',
                    label: '后台运行',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                },
                {
                    name: 'no-recreate',
                    label: '不重新创建已存在的容器',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                },
                {
                    name: 'timeout',
                    label: '超时时间(秒)',
                    type: 'number',
                    defaultValue: 60,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s up {scale} {detach} {no-recreate} {timeout}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '下线',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'volumes',
                    label: '删除卷',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                },
                {
                    name: 'remove-orphans',
                    label: '删除孤立容器',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s down {volumes} {remove-orphans}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },

        {
            label: '暂停',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'timeout',
                    label: '超时时间(秒)',
                    type: 'number',
                    defaultValue: 60,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s stop {timeout}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '恢复',
            when: ['docker-compose'],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s start',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '重启',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'timeout',
                    label: '超时时间(秒)',
                    type: 'number',
                    defaultValue: 60,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s restart {timeout}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '重新构建',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'pull',
                    label: '从远程拉取镜像',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                },
                {
                    name: 'no-cache',
                    label: '不使用缓存',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s build {pull} {no-cache}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '查看日志',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'follow',
                    label: '实时跟踪日志',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                },
                {
                    name: 'tail',
                    label: '显示日志条数',
                    type: 'number',
                    defaultValue: 100,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s logs {follow} {tail}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '查看容器',
            when: ['docker-compose'],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s ps',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '进入终端',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'service',
                    label: '服务名称',
                    type: 'text',
                    required: true,
                    isSingle: true
                },
                {
                    name: 'command',
                    label: '要执行的命令',
                    defaultValue: '/bin/bash',
                    type: 'text',
                    required: true,
                    isSingle: true
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s exec {service} {command}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '执行命令',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'service',
                    label: '服务名称',
                    type: 'text',
                    required: true,
                    isSingle: true
                },
                {
                    name: 'command',
                    label: '要执行的命令',
                    defaultValue: 'date',
                    type: 'text',
                    required: true,
                    isSingle: true
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s run {service} {command}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '拉取镜像',
            when: ['docker-compose'],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s pull',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '推送镜像',
            when: ['docker-compose'],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s push',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '查看配置',
            when: ['docker-compose'],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s config',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '验证配置',
            when: ['docker-compose'],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s validate',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        },
        {
            label: '移除停止的容器',
            when: ['docker-compose'],
            helps: [
                {
                    name: 'force',
                    label: '强制删除',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                },
                {
                    name: 'volumes',
                    label: '删除卷',
                    type: 'checkbox',
                    defaultValue: false,
                    required: false
                }
            ],
            onClick(node) {
                const template = format(
                    'cd %s && ls && sudo docker compose  -f %s rm {force} {volumes}',
                    node.parentPath,
                    node.path
                )
                send({
                    node,
                    helps: this.helps,
                    template,
                    isCommand: false
                })
            }
        }
    ]
}
